package id.co.rho.scoring.cid.client.mvc.typemodelconfigsearch;

import java.util.List;

import id.co.rho.scoring.cid.client.command.CommandDispatcherAsync;
import id.co.rho.scoring.cid.client.command.impl.TypeModelConfigCommand;
import id.co.rho.scoring.cid.client.command.impl.TypeModelConfigResult;
import id.co.rho.scoring.cid.client.command.impl.TypeModelConfigCommand.CommandType;
import id.co.rho.scoring.cid.client.managed.request.ApplicationRequestFactory;
import id.co.rho.scoring.cid.client.mvc.AppEvents;
import id.co.rho.scoring.cid.client.security.Credential;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.Info;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;

public class TypeModelConfigSearchView extends View {
	private CommandDispatcherAsync dispatcher;
	private TypeModelConfigSearchUI ui;
	private Credential credential;


	@Inject
	public TypeModelConfigSearchView(ApplicationRequestFactory requestFactory,
			CommandDispatcherAsync dispacther,Credential credential , final TypeModelConfigSearchUI ui) {
		super(null);
		this.dispatcher = dispacther;
		this.ui = ui;
		this.credential=credential;
		controller = new ViewController();
	}

	@Override
	public void initialize() {
		super.initialize();
		ui.initialize();
		ui.getSearchButton().addSelectionListener(
				new SelectionListener<ButtonEvent>() {

					@Override
					public void componentSelected(ButtonEvent ce) {
						ui.clearData();
						searchTypeModelConfig();
					}
				});

		ui.getGrid().addListener(Events.CellDoubleClick,
				new Listener<BaseEvent>() {
					public void handleEvent(BaseEvent be) {
						GridEvent ge = (GridEvent) be;
	                    AppEvent appEvent = new AppEvent(AppEvents.TypeModelConfigView);
	                    appEvent.setHistoryEvent(true);
	                    appEvent.setData("id",ge.getModel().get("id"));
	                    appEvent.setData("name",ge.getModel().get("name"));
	                    appEvent.setData("code",ge.getModel().get("code"));
	                    appEvent.setData("descr",ge.getModel().get("descr"));
	                    Dispatcher.forwardEvent(appEvent);
					}
				});
		ui.getDeleteButton().addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				long id =ui.getGrid().getSelectionModel().getSelectedItem().get("id");
				deleteData(id);
			}
		});
		ui.getAddButton().addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
                AppEvent appEvent = new AppEvent(AppEvents.TypeModelConfigView);
                Dispatcher.forwardEvent(appEvent);
				
			}
		});
		
		ui.getEditButton().addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
                AppEvent appEvent = new AppEvent(AppEvents.TypeModelConfigView);
                ModelData md= ui.getGrid().getSelectionModel().getSelectedItem();
                appEvent.setHistoryEvent(true);
                appEvent.setData("id",md.get("id"));
                appEvent.setData("name",md.get("name"));
                appEvent.setData("code",md.get("code"));
                appEvent.setData("descr",md.get("descr"));
                Dispatcher.forwardEvent(appEvent);
				
			}
		});
	}
	
	private void deleteData(long id) {
		TypeModelConfigCommand cmd = new TypeModelConfigCommand();
		cmd.setCommandType(CommandType.DELETE);
		cmd.setDeletedby(credential.getUsername());
		cmd.setId(id);
		dispatcher.execute(cmd, new AsyncCallback<TypeModelConfigResult>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(TypeModelConfigResult result) {
				if (result.isSaved() == initialized) {
					Info.display("Deleted", "Data has been deleted");
					AppEvent appEvent = new AppEvent(
							AppEvents.TypeModelConfigSearchView);
					Dispatcher.forwardEvent(appEvent);
				}
			}

		});
	}


	@Override
	protected void handleEvent(AppEvent event) {
		if (event.getType().equals(AppEvents.TypeModelConfigSearchView)) {
			ui.show();
			searchTypeModelConfig();
		}
	}

	public void searchTypeModelConfig() {
		TypeModelConfigCommand cmd = new TypeModelConfigCommand();
		cmd.setCode(ui.getTxtCode().getValue());
		cmd.setName(ui.getTxtName().getValue());
		cmd.setCommandType(CommandType.SEARCH);
		dispatcher.execute(cmd, new AsyncCallback<TypeModelConfigResult>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(TypeModelConfigResult result) {
				ui.setModels((List<ModelData>) result.getModels());
			}

		});

	}

	private class ViewController extends Controller {
		private ViewController() {
			registerEventTypes(AppEvents.TypeModelConfigSearchView);
		}

		@Override
		public void handleEvent(AppEvent event) {
			forwardToView(TypeModelConfigSearchView.this, event);
		}
	}

}
